package aod.android.com.bernay.mine.fragment;

import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Window;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseFragment;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.activity.AlbumActivity;
import aod.android.com.bernay.mine.adapter.AlbumAdapter;
import aod.android.com.bernay.mine.module.AlbumBean;
import aod.android.com.bernay.mine.module.AlbumInnerBean;
import aod.android.com.bernay.mine.module.MyAlbumBean;
import aod.android.com.bernay.util.MessageEvent;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.util.Utils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/12 14:25
 * @Email jamesKing0812@gmail.com
 */
public class AlbumFragment extends BernayBaseFragment {

    @HiView(R.id.albumRecycleView)
    RecyclerView mAlbumRecycleView;

    private AlbumAdapter albumAdapter;

    public boolean isSelectAll = false;
    private final static int pageIndex = 1;
    private final static int pageSize = 12;
    private MyAlbumBean albumBean;
    public List<AlbumBean> mAlbumDatas = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.fragment_album;
    }

    @Override
    protected void initData() {

        getMyAlbumData();
        //布局样式
        mAlbumRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAlbumDatas.addAll(AlbumBean.mAlbumBeans);
        //分割线
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.divider_mileage));
        mAlbumRecycleView.addItemDecoration(divider);
        //添加适配器
        albumAdapter = new AlbumAdapter(getActivity(),mAlbumDatas);
        mAlbumRecycleView.setAdapter(albumAdapter);

        //点击事件
        albumAdapter.setOnItemClickListener(new AlbumAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //TODO
            }
        });
    }

    /**
     * 获取我的相册数据
     */
    private void getMyAlbumData() {
        String userId = SharePreUtils.newInstance(getActivity()).getUserId();
        String token =  SharePreUtils.newInstance(getActivity()).getToken();
        BernayApi.http().getMyAlbum(userId, token, pageIndex, pageSize, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                albumBean = gson.fromJson(json, MyAlbumBean.class);
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }

    //全选
    public void initSelect(){
        isSelectAll = !isSelectAll;
        if (isSelectAll) {
            for (int i = 0; i < mAlbumDatas.size(); i++) {
                List<AlbumInnerBean> albumInnerBeans = mAlbumDatas.get(i).getAlbumList();
                for (int y = 0; y < albumInnerBeans.size(); y++) {
                    AlbumInnerBean albumInnerBean = albumInnerBeans.get(y);
                    albumInnerBean.setSelect(true);
                }
            }
        }else{
            cancelSelected();
        }
        albumAdapter.notifyDataSetChanged();
    }

    //取消全选
    public void unSelected(){
        isSelectAll = false;
        if (!isSelectAll){
            cancelSelected();
        }
        albumAdapter.notifyDataSetChanged();
    }

    /**
     * 反选择
     */
    public void cancelSelected(){
        for (int i = 0; i < mAlbumDatas.size(); i++) {
            List<AlbumInnerBean> albumInnerBeans = mAlbumDatas.get(i).getAlbumList();
            for (int y = 0; y < mAlbumDatas.get(i).getAlbumList().size(); y++) {
                AlbumInnerBean albumInnerBean = albumInnerBeans.get(y);
                albumInnerBean.setSelect(false);
            }
        }
    }

    //获取选择数量以及列表数据总数量
    public String getListSize(){
        List<String> listSize = new ArrayList<>();
        for (int i = 0; i < mAlbumDatas.size(); i++) {
            for (int j = 0; j < mAlbumDatas.get(i).getAlbumList().size(); j++) {
                listSize.add("选中"+j);
            }
        }
        return listSize.size()+"";
    }


    //获取数组数据
    public void getListItems(){
        Utils.selectedList.clear();
        for (int i = 0; i < mAlbumDatas.size(); i++) {
            for (int j = 0; j < mAlbumDatas.get(i).getAlbumList().size(); j++) {
                Utils.selectedList.add("选中"+j);
            }
        }
    }

    //控制全选按钮展示隐藏
    public void controlCheck(boolean isShow){
        albumAdapter.showSelected(isShow);
    }
}
